﻿using System.Data;
using System.Data.SqlClient;

namespace Adams.Web.Data.PressReleases
{
    public static class Index
    {
        public static Models.PressReleases.Index.PageLoad PageLoad(int page, int pagesize)
        {
            var data = new Models.PressReleases.Index.PageLoad() { 
                Page = page,
                PageSize = pagesize,
                PressReleases = new Shared.Entities.PressReleases.PressReleaseCollection()
            };

            int startrow = (pagesize * (page - 1));
            string dbq = DB.BuildPagingQuery(startrow, pagesize, "*", "PressReleases.PressReleases", string.Empty, string.Empty, "ReleaseDate DESC", "PressReleaseID");

            using (SqlDataReader dr = DB.RunDataReader(dbq, CommandType.Text))
            {
                while (dr.Read())
                {
                    var PressRelease = DB.InitFromReader<Shared.Entities.PressReleases.PressRelease>(dr, new ColMap { NameInDb = "PressReleaseID", NameInObject = "ID" });
                    data.PressReleases.Add(PressRelease);
                }

                dr.NextResult();
                dr.Read();
                data.RecordCount = dr[0].DbInt();
            }

            return data;
        }
    }
}
